﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Ionic.Zip;
using PocketEngine.Engine;
using PocketEngine.Images;
namespace PocketEngine.Zips
{
    public class pZip
    {
        ZipFile Zip;
        /// <summary>
        /// Open a zip for data extraction.  
        /// </summary>
        /// <param name="path"></param>
        /// 
        public pZip(string path)
        {
            Zip = new ZipFile(path);
         
        }

        /// <summary>
        /// Creates a new zip pakcage. Call save to finalize it.
        /// </summary>
        /// <param name="path"></param>
        /// <param name="comment"></param>
        /// 
        public pZip(string path, string comment)
        {
            if (new FileInfo(path).Exists)
            {
                FileInfo fi = new FileInfo(path);
                fi.Delete();
            }


            Zip = new ZipFile(path);
            Zip.Comment = comment;
            Zip.SaveProgress += new EventHandler<SaveProgressEventArgs>(zip_SaveProgress);
          
            using (ZipFile zip = Zip)
            {

            }

        }
        public void Save()
        {
            using (ZipFile zip = Zip)
            {
                Console.WriteLine("Saving Zip.");
                zip.Save();
                Console.WriteLine("Saved");
            }
        }
        public void SaveProgress()
        {
            using (ZipFile zip = Zip)
            {

            }
        }

        void zip_SaveProgress(object sender, SaveProgressEventArgs e)
        {
            Console.Clear();
            Console.WriteLine("Saving Zip. " + e.EntriesSaved + "/" + e.EntriesTotal);
            //throw new NotImplementedException();
        }
        public void Add(string id, string content, string comment)
        {
           ZipEntry ze = Zip.AddFileFromString(id, "Text/", content);
     
        }
        private string mPassword;

        public string GetString(string id)
        {
            string path = "Text/";
            Stream s = _Get(id, path);
            char[] txt = new char[s.Length];
            for (int i = 0; i < txt.Length; i++)
            {
                txt[0] = (char)s.ReadByte();
            }
            return new string(txt);
        }
        public pImage GetImage(string id)
        {
            string imgID = GetString(id);

            pImage img = null;

            string[] tok = Text.pTokenizer.Tokenize(imgID);
            Console.WriteLine("Original:" + imgID);
            foreach (var t in tok)
            {
                Console.WriteLine("Token:" + t);
            }

            Environment.Exit(-1);
            return img;
        }
        private Stream _Get(string id, string path)
        {
            ZipEntry ze = Zip[path + id];
            if (ze == null)
            {
                pEngine.Err("Unable to find zip entry:" + id, "pZip.getString");
            }
            MemoryStream s = new MemoryStream(1024 * 100);
       
            ze.Extract(s);
          
            return s;
        }
        public void Add(string id, pImage img, string comment)
        {
            using (ZipFile zip = Zip)
            {

                string imgID = string.Empty;
                StringBuilder sb = new StringBuilder();

                sb.AppendLine("Path(" + img.Path + ")");
                sb.AppendLine("Width(" + img.Width + ")");
                sb.AppendLine("Height(" + img.Height + ")");
                sb.AppendLine("BytesPerPixel(" + img.BytesPerPixel + ")");
                if (img.Normals != null)
                {
                    sb.AppendLine("true");
                    sb.AppendLine("Path(" + img.Normals.Path + ")");
                    sb.AppendLine("Width(" + img.Normals.Width + ")");
                    sb.AppendLine("Height(" + img.Normals.Height + ")");
                    sb.AppendLine("BytesPerPixel(" + img.Normals.BytesPerPixel + ")");
                }
                else
                {
                    sb.AppendLine("false");
                }
                if (img.Speculars != null)
                {
                    sb.AppendLine("true");
                    sb.AppendLine("Path(" + img.Speculars.Path + ")");
                    sb.AppendLine("Width(" + img.Speculars.Width + ")");
                    sb.AppendLine("Height(" + img.Speculars.Height + ")");
                    sb.AppendLine("BytesPerPixel(" + img.Speculars.BytesPerPixel + ")");
                }
                else
                {
                    sb.AppendLine("false");
                }
                imgID = sb.ToString();
                ZipEntry ze = zip.AddFileFromString(id, "Images/", imgID);
               
                zip.AddFileFromStream(img.Path, "Images/Diffuse/", img.ToStream());
                if (img.Normals != null)
                {
                    zip.AddFileFromStream(img.Normals.Path, "Images/Normal/", img.Normals.ToStream());
                
                }
                if (img.Speculars != null)
                {
                    zip.AddFileFromStream(img.Speculars.Path, "Images/Specular/", img.Speculars.ToStream());
                }
            }
        }

    }
}
